<!-- 系统基础配置页面 -->
<template>
  <div>
    <!-- 设置页面组件的大小 -->
    <div>
      配置项目中的组件大小
      <el-radio-group v-model="size">
        <el-radio label="medium">中等组件大小</el-radio>
        <el-radio label="small">小等组件大小</el-radio>
        <el-radio label="mini">超小组件大小</el-radio>
      </el-radio-group>
    </div>
    <!-- 新增按钮 -->
    <div>
      <el-button @click="toAdd" class="btn" type="primary">新增</el-button>
    </div>
    <!-- 表格数据展示 -->
    <!-- {{tableData}} -->
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="名称" align="center">
        </el-table-column>
        <el-table-column prop="val" label="值" align="center" width="120px">
          <template slot-scope="scope">
            <el-image
              v-if="
                scope.row.val ? scope.row.val.indexOf('http://') != -1 : false
              "
              :src="scope.row.val"
            />
            <span v-else class="text">{{
              scope.row.val ? scope.row.val : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="introduce" label="介绍"> </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button @click="toEdit(scope.row)" type="text">编辑</el-button>
            <el-button @click="toDelete(scope.row.id)" type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增、编辑对话框 -->
    <div>
      <el-dialog :title="title" :visible.sync="visible" @close="toClose">
        <el-form :model="form" :rules="rules" ref="ruleForm">
          <el-form-item prop="name" label="名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            prop="introduce"
            label="介绍"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.introduce" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型" :label-width="formLabelWidth">
            <el-select clearable v-model="selectType" @change="typeChange">
              <el-option label="文字" value="文字"></el-option>
              <el-option label="图片" value="图片"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="值" :label-width="formLabelWidth">
            <el-upload
              v-if="selectType === '图片'"
              class="avatar-uploader"
              :action="uploadFileURL"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
            <el-input v-model="form.val" v-else autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { showFileURL, uploadFileURL } from "@/utils/config";
import Cookies from "js-cookie";
import { findAll, saveOrUpdate, deleteById } from "@/api/baseConfig";
export default {
  data() {
    return {
      size: Cookies.get("size"),
      showFileURL,
      uploadFileURL,
      tableData: [],
      form: {},
      title: "",
      // 选择类型
      selectType: "",
      //表单控件标题的宽度
      formLabelWidth: "80px",
      imageUrl: "",
      visible: false,
      // 校验规则
      rules: {
        name: [{ required: true, message: "请输轮播图名称", trigger: "blur" }],
        introduce: [
          { required: true, message: "请输入轮播图介绍信息", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    size(newValue, oldValue) {
      this.toChangeSize(newValue);
    },
  },
  created() {
    this.getBaseConfigData();
  },
  methods: {
    // 设置cookie中的size的大小
    toChangeSize(size) {
      Cookies.set("size", size);
      this.$confirm("是否刷新页面", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 刷新页面
          location.reload();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "请手动刷新页面让配置生效",
          });
        });
    },

    // 查询系统基础配置信息
    async getBaseConfigData() {
      let res = await findAll();
      this.tableData = res.data;
    },
    toAdd() {
      this.visible = true;
      this.title = "新增基础配置";
      this.form = {};
      this.selectType = "文字";
    },
    // 类型发生改变时
    typeChange() {
      this.form.val = "";
    },
    toEdit(row) {
      if (row.val.indexOf("http://") != -1) {
        this.selectType = "图片";
        this.imageUrl = row.val;
      } else {
        this.selectType = "文字";
      }
      this.visible = true;
      this.title = "编辑基础配置";
      this.form = { ...row };
    },
    // 新增/编辑模态框的确定按钮点击处理事件
    toSave() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await saveOrUpdate(this.form);
          // console.log(res);
          if (res.status == 200) {
            this.$notify({
              title: "保存成功",
              message: res.message,
              type: "success",
            });
          }
          this.visible = false;
          this.getBaseConfigData();
        } else {
          return false;
        }
      });
    },
    toClose() {
      this.$refs.ruleForm.resetFields();
    },
    // 删除按钮
    async toDelete(id) {
     
      this.$confirm("此操作将永久删除这条数据,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteById({id});
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功",
            });
          }
          this.getBaseConfigData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //图片上传成功的回调函数
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      // this.form.photo = showFileURL + res.data.id;
      // 从图片服务器请求图片路径存到表单
      this.form.val = file.response;
    },
    //图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped>
</style>
